package application;

/**
 * Created by DN on 20.05.2014.
 */
public class ArraySearch {
    public static int binarySearch(int[] a, int key) {
        int lo = 0;
        int hi = a.length - 1;
        int iteration=0;
        while (lo <= hi) {
            iteration++;
            int mid = lo + (hi - lo) / 2;
            if (key < a[mid])
                hi = mid - 1;
            else {
                if (key > a[mid])
                    lo = mid + 1;
                else
                    return iteration;//mid;
            }
        }
        return -1;
    }

    public void sort (int[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - 1; j++) {
                if (a[j] > a[j + 1]) {
                    int d = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = d;
                }
            }
        }
    }
}
